package com.tongcao.cn.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 国际化语言工具类
 *
 * @author Bookers
 * @date 2017/07/06
 */
public class LocalizationUtils {

    final static public String defaultLocale = "zh_CN";

    final static String basename = "i18n/ebooking";

    final static Map<String, ResourceBundle> cachedResourceBundles = new HashMap<>();

    final static List<String> localeList;

    static {
        localeList = new ArrayList<>();
        localeList.add(defaultLocale);
        localeList.add("en_US");
    }

    /**
     * 获取语言包
     *
     * @param localeStr 语言
     * @return JSON
     */
    public static String getAll(String localeStr) {
        if (StringUtils.isEmpty(localeStr) || !localeList.contains(localeStr)) {
            localeStr = defaultLocale;
        }

        JSONObject languageJson = new JSONObject();

        ResourceBundle resourceBundle = getResourceBundle(localeStr);
        if (resourceBundle == null) {
            return languageJson.toJSONString();
        }

        for (String key : resourceBundle.keySet()) {
            languageJson.put(key, resourceBundle.getObject(key));
        }
        return languageJson.toJSONString();
    }


    public static String getLocale(String key, String localeStr, String msg) {
        String localizationMsg = getLocale(key, localeStr);
        if (StringUtils.isEmpty(localizationMsg)) {
            return msg;
        }
        return localizationMsg;
    }

    /***
     * 翻译
     *
     * @param key
     * @param localeStr 语言
     * @return
     */
    public static String getLocale(String key, String localeStr) {
        if (StringUtils.isEmpty(localeStr) || !localeList.contains(localeStr)) {
            localeStr = defaultLocale;
        }

        if (StringUtils.isEmpty(key)) {
            return "";
        }

        ResourceBundle resourceBundle = getResourceBundle(localeStr);
        if (resourceBundle == null) {
            return "";
        }

        try {
            if (resourceBundle.containsKey(key)) {
                return resourceBundle.getString(key);
            }
        } catch (Exception e) {
        }
        return "";
    }

    /**
     * 获取语言包
     *
     * @param localeStr
     * @return ResourceBundle
     */
    private static ResourceBundle getResourceBundle(String localeStr) {
        ResourceBundle resourceBundle;
        //缓存
        if (cachedResourceBundles.containsKey(localeStr)) {
            resourceBundle = cachedResourceBundles.get(localeStr);
        } else {
            String[] temp = localeStr.split("_");


            Locale locale;
            if (temp.length == 2) {
                locale = new Locale(temp[0], temp[1]);
            } else {
                locale = new Locale(temp[0]);
            }
            try {
                resourceBundle = ResourceBundle.getBundle(basename, locale);
                if (resourceBundle != null) {
                    cachedResourceBundles.put(localeStr, resourceBundle);
                }
            } catch (Exception e) {
                return null;
            }
        }
        return resourceBundle;
    }

    public static String get(String key, String text) {
        //TODO
        if (StringUtils.isNotEmpty(text)) {
            return text;
        } else {
            return "不存在";
        }
    }

   /* *//**
     * 国际化-月
     *
     * @param month 月份
     * @return
     *//*
    public static String getMonth(String month) {
        String locale = ShiroPermissUtils.getUser().getLanguage();
        return getMonth(month, locale);
    }

    public static String getMonth(String month, String locale) {
        String monthLocale = "";
        switch (month) {
            case "1":
                monthLocale = LocalizationUtils.getLocale("view.data.January", locale);
                break;
            case "2":
                monthLocale = LocalizationUtils.getLocale("view.data.February", locale);
                break;
            case "3":
                monthLocale = LocalizationUtils.getLocale("view.data.March", locale);
                break;
            case "4":
                monthLocale = LocalizationUtils.getLocale("view.data.April", locale);
                break;
            case "5":
                monthLocale = LocalizationUtils.getLocale("view.data.May", locale);
                break;
            case "6":
                monthLocale = LocalizationUtils.getLocale("view.data.June", locale);
                break;
            case "7":
                monthLocale = LocalizationUtils.getLocale("view.data.July", locale);
                break;
            case "8":
                monthLocale = LocalizationUtils.getLocale("view.data.August", locale);
                break;
            case "9":
                monthLocale = LocalizationUtils.getLocale("view.data.September", locale);
                break;
            case "10":
                monthLocale = LocalizationUtils.getLocale("view.data.October", locale);
                break;
            case "11":
                monthLocale = LocalizationUtils.getLocale("view.data.November", locale);
                break;
            case "12":
                monthLocale = LocalizationUtils.getLocale("view.data.December", locale);
                break;
            default:
        }
        return monthLocale;
    }
*/
//    /**
//     * 国际化-年
//     *
//     * @param year
//     * @return
//     */
//    public static String getYear(String year) {
//        String locale = ShiroPermissUtils.getUser().getLanguage();
//        return getYear(year, locale);
//    }

    public static String getYear(String year, String locale) {
        if (StringUtils.isEmpty(locale) || defaultLocale.equalsIgnoreCase(locale)) {
            return year + "年";
        }
        return year;
    }

}


